package com.eastseeway.commonlib_esw;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class EngineerActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_engineer);
        registerLocalReceiver();
    }

    public void registerLocalReceiver() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
        filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
        filter.addAction(Intent.ACTION_MEDIA_SHARED);//如果SDCard未安装,并通过USB大容量存储共享返回
        filter.addAction(Intent.ACTION_MEDIA_MOUNTED);//表明sd对象是存在并具有读/写权限
        filter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);//SDCard已卸掉,如果SDCard是存在但没有被安装
        filter.addAction(Intent.ACTION_MEDIA_CHECKING);  //表明对象正在磁盘检查
        filter.addAction(Intent.ACTION_MEDIA_EJECT);  //物理的拔出 SDCARD
        filter.addAction(Intent.ACTION_MEDIA_REMOVED);  //完全拔出
        filter.addDataScheme("file"); // 必须要有此行，否则无法收到广播
        USBReceiver mUsbReceiver = new USBReceiver();
        registerReceiver(mUsbReceiver, filter);
    }

    private class USBReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            Log.e("12121","action: "+action);
            if (Intent.ACTION_MEDIA_MOUNTED.equals(action)) {
                //U盘插入
                String path = intent.getData().getPath();
//                path = getCorrectPath(path);//获取正确的，完整的路径
                File file = new File(path);
                for (File listFile : Objects.requireNonNull(file.listFiles())) {
                    if (listFile.isFile() && listFile.getName().contains("swtp")) {
                        Log.e("12121","Name "+listFile.getPath());
//                        ZtlManager.GetInstance().installApp(listFile.getPath());
                    }
                }
                if (!diskListenerList.isEmpty()){
                    for (int i = 0; i < diskListenerList.size(); i++) {
                        if (null != diskListenerList.get(i))diskListenerList.get(i).onConnect();
                    }
                }
            } else if (Intent.ACTION_MEDIA_UNMOUNTED.equals(action) || Intent.ACTION_MEDIA_EJECT.equals(action)) {
                //U盘拔出
                if (!diskListenerList.isEmpty()){
                    for (int i = 0; i < diskListenerList.size(); i++) {
                        if (null != diskListenerList.get(i))diskListenerList.get(i).onDisconnect();
                    }
                }
            }

            // 这里可以拿到插入的USB设备对象
//            UsbDevice usbDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
//            switch (Objects.requireNonNull(intent.getAction())) {
//                case UsbManager.ACTION_USB_DEVICE_ATTACHED: // 插入USB设备
//                    Log.e("12121","检测到usb设备插入 "+usbDevice.getDeviceName());
//                    List<String> usbPaths = USBUtil.getUsbPaths();
//                    for (String usbPath : usbPaths) {
//                        Log.e("12121","路径 "+usbPath);
//                    }
//                    break;
//                case UsbManager.ACTION_USB_DEVICE_DETACHED: // 拔出USB设备
//                    break;
//                default:
//                    break;
//            }
        }
    }


    /**
     * U盘连接状态回调
     */
    public interface IUDiskListener{
        void onConnect();
        void onDisconnect();
    }
    private List<IUDiskListener> diskListenerList = new ArrayList<>();
    public void setUDiskListener(IUDiskListener uDiskListener){
        diskListenerList.add(uDiskListener);
    }
    public void removeUDiskListener(IUDiskListener uDiskListener){
        diskListenerList.remove(uDiskListener);
    }
//    private String getCorrectPath(String path) {
//        if (!TextUtils.isEmpty(path)){
//            int lastSeparator = path.lastIndexOf(File.separator);
//            String endStr = path.substring(lastSeparator + 1, path.length());
//            if (!TextUtils.isEmpty(endStr) && (endStr.contains("USB_DISK") || endStr.contains("usb_disk"))){//不区分大小写
//                File file = new File(path);
//                if (file.exists() && file.listFiles().length == 1 && file.listFiles()[0].isDirectory()){
//                    path = file.listFiles()[0].getAbsolutePath();
//                }
//            }
//        }
//        return path;
//    }
}